
#include "Canvas.h"
#include <iostream>
#include "utils.h"

Canvas::Canvas()
    : shapes_(std::list<Shape*>()), selected_(std::list<Shape*>())
{

}

void Canvas::AddShape(Shape* s)
{
  utils::Assertnnull(s, "参数类型为Shape *的参数不能为nullptr");
  shapes_.push_back(s);
}

void Canvas::Show()
{
  for (auto s : shapes_) {
    s->Draw();
  }
}

Shape* Canvas::Select(int i)
{
  if (i >= shapes_.size() || i < 0) {
    std::cout << "下标越界" << std::endl;
    return nullptr;
  }
  int k = 0;
  auto from = shapes_.begin(), to = shapes_.end();
  for (; from != to && i != k; from++, k++); // empty
  selected_.push_back(*from);
  return *from;
}

void Canvas::OnDrag()
{
  for (auto s : selected_) {
    shapes_.push_back(s->Clone());
  }
  // 清空_selected
  selected_.clear();
}

Canvas::~Canvas()
{
  selected_.clear();

  for (auto s : shapes_) {
    delete s;
  }

  shapes_.clear();
}
